package com.colin.dubbo.datasource;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataSourceAspect {
	
	private static final Logger logger = LoggerFactory.getLogger(DataSourceAspect.class);

	public void dataSourceCut(JoinPoint joinPoint) {
		Object pointTarget = joinPoint.getTarget();
		String methodName = joinPoint.getSignature().getName();
		Class<?>[] parameterTypes = ((MethodSignature) joinPoint.getSignature()).getMethod().getParameterTypes();
		try {
			Method method = pointTarget.getClass().getMethod(methodName, parameterTypes);
			if (method.isAnnotationPresent(DataSource.class)) {
				DataSource dataSource = method.getAnnotation(DataSource.class);
				if(logger.isDebugEnabled()) {
					logger.info(" DataSourceBridge { dataSourceType : " + dataSource.dataSourceType().name() + " , method : " + methodName + " } ");
				}
				DataSourceHolder.setDataSource(dataSource.dataSourceType().name());
			}

		} catch (Exception e) {
			logger.error("dataSourceCut_error", e);
		}
	}

}
